package com.gdones.wms.bus.record.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.fields.dto.WmsFieldsInfo;
import com.gdones.wms.bus.fields.mapper.WmsFieldsInfoMapper;
import com.gdones.wms.bus.fields.mapper.WmsFieldsMapper;
import com.gdones.wms.bus.inbound.dto.WmsInbound;
import com.gdones.wms.bus.inbound.dto.WmsInboundInfo;
import com.gdones.wms.bus.inbound.mapper.WmsInboundInfoMapper;
import com.gdones.wms.bus.inbound.mapper.WmsInboundMapper;
import com.gdones.wms.bus.outbound.dto.WmsOutbound;
import com.gdones.wms.bus.outbound.dto.WmsOutboundInfo;
import com.gdones.wms.bus.outbound.mapper.WmsOutboundInfoMapper;
import com.gdones.wms.bus.outbound.mapper.WmsOutboundMapper;
import com.gdones.wms.bus.record.dto.RecordDto;
import com.gdones.wms.bus.record.dto.WmsRecord;
import com.gdones.wms.bus.record.dto.WmsRecordLife;
import com.gdones.wms.bus.record.mapper.WmsRecordLifeMapper;
import com.gdones.wms.bus.record.mapper.WmsRecordMapper;
import com.gdones.wms.bus.record.service.IWmsRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.wms.bus.warehouse.dto.WmsWarehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-08
 */
@Service
public class WmsRecordServiceImpl extends ServiceImpl<WmsRecordMapper, WmsRecord> implements IWmsRecordService {

    @Autowired
    private WmsInboundMapper inboundMapper;

    @Autowired
    private WmsInboundInfoMapper inboundInfoMapper;

    @Autowired
    private WmsOutboundMapper outboundMapper;

    @Autowired
    private WmsOutboundInfoMapper outboundInfoMapper;

    @Autowired
    private WmsFieldsInfoMapper fieldsInfoMapper;

    @Autowired
    private WmsRecordLifeMapper lifeMapper;

    @Override
    public MyResult pages(WmsRecord record, MyPageBand pageBand) {

        MyResult result = new MyResult();

        WmsRecordMapper wmsRecordMapper = getBaseMapper();

        IPage<WmsRecord> pages = wmsRecordMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), record);

        // 根据分页的结果封装返回数据
        MyPager<WmsRecord> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult list(WmsRecord record) {

        MyResult result = new MyResult();

        WmsRecordMapper wmsRecordMapper = getBaseMapper();

        List<WmsRecord> list = wmsRecordMapper.myList(record);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult getByID(String recordID) {

        MyResult result = new MyResult();

        RecordDto resDto = new RecordDto();

        WmsRecord record = getById(recordID);

        if(record!=null){
            // 查询补充字段描述
            QueryWrapper<WmsFieldsInfo> fieldsInfoQueryWrapper = new QueryWrapper<>();
            fieldsInfoQueryWrapper.eq("busID",recordID);

            List<WmsFieldsInfo> wmsFieldsInfos = fieldsInfoMapper.selectList(fieldsInfoQueryWrapper);

            resDto.setFieldsInfoList(wmsFieldsInfos);
            resDto.setRecord(record);
            result.setData(resDto);
            result.setMsg("查询成功！");
        }else{
            result.setMsg("查询失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult add(RecordDto dto) {

        MyResult result = new MyResult();

        // 要保存的物资信息(物资信息)
        WmsRecord record = dto.getRecord();
        record.setRecordID(IdUtil.fastSimpleUUID());
        record.setCode("WLR-"+ MyPinYinUtil.getPinYinByString(record.getName())+"-"+new Date().getTime());

        // 入库单信息前端只需要选择一个入库人即可，其他的自动生成(入库单信息)
        WmsInbound inbound = dto.getInbound();
        inbound.setInboundID(IdUtil.fastSimpleUUID());
        inbound.setName(record.getName()+"-物资入库单-"+new Date().getTime());
        inbound.setCode("WMIN-"+ MyPinYinUtil.getPinYinByString(record.getName())+"-"+new Date().getTime());
        inbound.setInboundTime(new Date());
        inbound.setWarehouseID(record.getWarehouseID());
        inbound.setWarehouseName(record.getWarehouseName());
        inbound.setRecordTypeCount(1);
        inbound.setInCount(record.getWmsCount());

        // 准备入库单明细
        WmsInboundInfo inboundInfo = new WmsInboundInfo();
        inboundInfo.setInboundID(inbound.getInboundID());
        inboundInfo.setRecordID(record.getRecordID());
        inboundInfo.setRecordName(record.getName());
        inboundInfo.setInCount(record.getWmsCount());

        // 保存物资关联的附加字段信息（附加字段信息）
        List<WmsFieldsInfo> fieldsInfoList = dto.getFieldsInfoList();
        for(WmsFieldsInfo info : fieldsInfoList){
           info.setBusID(record.getRecordID());
        }

        // 准备记录信息
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WmsRecordLife life = new WmsRecordLife();
        life.setRecordID(record.getRecordID());
        life.setRecordName(record.getName());
        life.setLifeType("物资入库");
        life.setLifeTime(new Date());
        life.setLifeDesc(record.getName()+"("+record.getCode()+")，入库数量：【"+record.getWmsCount()+"】，入库到："+record.getWarehouseName()+"，入库时间："+(sdf.format(inbound.getInboundTime()))+"，入库人："+inbound.getUserName());

        // 保存物资信息
        this.save(record);
        // 保存入库单信息
        inboundMapper.insert(inbound);
        // 保存入库单明细
        inboundInfoMapper.insert(inboundInfo);
        // 保存附属字段
        for(WmsFieldsInfo info : fieldsInfoList){
            fieldsInfoMapper.insert(info);
        }
        // 保存记录信息
        lifeMapper.insert(life);

        result.setMsg("台账添加成功！");

        return result;
    }

    @Override
    public MyResult update(RecordDto dto) {

        MyResult result = new MyResult();

        WmsRecord record = dto.getRecord();

        WmsRecordMapper recordMapper = getBaseMapper();

        recordMapper.updateById(record);

        // 先删除
        fieldsInfoMapper.realDeletebyBusID(record.getRecordID());

        // 再添加
        List<WmsFieldsInfo> fieldsInfoList = dto.getFieldsInfoList();
        for(WmsFieldsInfo info : fieldsInfoList){
            info.setBusID(record.getRecordID());
            fieldsInfoMapper.insert(info);
        }

        // 修改信息记录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WmsRecordLife life = new WmsRecordLife();
        life.setRecordID(record.getRecordID());
        life.setRecordName(record.getName());
        life.setLifeType("物资信息修改");
        life.setLifeTime(new Date());
        life.setLifeDesc(record.getName()+"("+record.getCode()+"),在【"+sdf.format(new Date())+"】进行信息修改！");

        lifeMapper.insert(life);

        result.setMsg("台账信息修改成功！");

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> idList = Arrays.asList(split);

        // 失效信息记录
        for(String id: idList){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            WmsRecordLife life = new WmsRecordLife();
            life.setRecordID(id);
            life.setLifeType("物资设置为失效");
            life.setLifeTime(new Date());
            life.setLifeDesc("在【"+sdf.format(new Date())+"】物资设置为失效！");

            lifeMapper.insert(life);
        }

        boolean b = removeByIds(idList);
        if(b){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult outRecord(RecordDto dto) {

        MyResult result = new MyResult();

        WmsRecord record = dto.getRecord();
        // 先查询出旧的物资台账信息
        WmsRecord oldRecord = getById(record.getRecordID());
        // 台账数量减少
        oldRecord.setWmsCount(oldRecord.getWmsCount() - record.getWmsCount());

        // 准备出库单
        WmsOutbound outbound = new WmsOutbound();
        outbound.setName(record.getName()+"-物资出库单-"+new Date().getTime());
        outbound.setCode("WMOUT-"+ MyPinYinUtil.getPinYinByString(record.getName())+"-"+new Date().getTime());
        outbound.setOutboundID(IdUtil.fastSimpleUUID());
        outbound.setOutboundTime(new Date());
        outbound.setWarehouseID(oldRecord.getWarehouseID());
        outbound.setWarehouseName(oldRecord.getWarehouseName());
        outbound.setRecordTypeCount(1);
        outbound.setOutCount(record.getWmsCount());
        outbound.setUserID(dto.getOutbound().getUserID());
        outbound.setUserName(dto.getOutbound().getUserName());

        // 准备出库单明细
        WmsOutboundInfo outboundInfo = new WmsOutboundInfo();
        outboundInfo.setOutboundID(outbound.getOutboundID());
        outboundInfo.setRecordID(oldRecord.getRecordID());
        outboundInfo.setRecordName(oldRecord.getName());
        outboundInfo.setOutCount(record.getWmsCount());

        // 记录信息
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WmsRecordLife life = new WmsRecordLife();
        life.setRecordID(record.getRecordID());
        life.setRecordName(record.getName());
        life.setLifeType("物资出库");
        life.setLifeTime(new Date());
        life.setLifeDesc(record.getName()+"("+record.getCode()+")，出库数量：【"+record.getWmsCount()+"】，出库仓库："+record.getWarehouseName()+"，出库时间："+(sdf.format(outbound.getOutboundTime()))+"，出库人："+outbound.getUserName());

        // 更新台账信息
        updateById(oldRecord);
        // 出库信息保存
        outboundMapper.insert(outbound);
        outboundInfoMapper.insert(outboundInfo);

        // 记录信息保存
        lifeMapper.insert(life);

        result.setMsg("物资出库成功！");

        return result;
    }

    @Override
    public MyResult getLifeList(String recordID) {

        MyResult result = new MyResult();

        List<WmsRecordLife> list = lifeMapper.getLifeList(recordID);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }


}
